<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Access_luar extends MX_Controller {
    
  function __construct() {
//    $this->load->library('manimage');
    $this->load->model('frm/mfrm');
//    $this->menu = $this->cek();
  }
  function test(){
    $this->debug($this->input->post(), true);
  }
  function curl_sign_tanggungan_mahasiswa_baru(){
    
    /**
     * kunci
     * id_course_category
     * id_kelompok_mahasiswa
     * type
     * nim
     * notransaksi
     * semester
     * tahun_ajaran
     * users
     */
    $result = "Gagal";
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == 'nubusa'){
      $jenis_biaya = $this->global_models->get("keuangan_mahasiswa_jenis_biaya");
      $pecent = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", "percent", 
            array("id_course_category" => $pst['id_course_category'], "id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], "type" => $pst['type']));
      $total_tanggungan = 0;
      
      $this->global_models->get_connect("zikula");
      $mahasiswa = $this->global_models->get("academicportal_mahasiswa", array("pn_uid" => $pst['users']));
      $ref_number = $this->global_models->get_field("academicportal_ao_applicant", "ref_number", array("id" => $mahasiswa[0]->ao_applicant_id));
      $this->global_models->get_connect("default");
      
//      set transaksi saat calon mahasiswa
      $tanggungan_mahasiswa_baru = $this->global_models->get("keuangan_mahasiswa_baru_tanggungan", array("id_ao_applicant" => $mahasiswa[0]->ao_applicant_id));
      foreach($tanggungan_mahasiswa_baru AS $tmb){
        $kirim[] = array(
            "nim"                                 => $pst['nim'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $tmb->id_keuangan_mahasiswa_jenis_virtual_account,
            "id_keuangan_mahasiswa_jenis_biaya"   => $tmb->id_keuangan_mahasiswa_jenis_biaya,
            "notransaksi"                         => $tmb->notransaksi,
            "saldo"                               => $tmb->saldo,
            "pos"                                 => $tmb->pos,
            "id_semester"                         => $tmb->id_semester,
            "tahun"                               => $tmb->tahun,
            "note"                                => $tmb->note,
            "create_date"                         => $tmb->create_date
        );
        if($tmb->pos == 2){
          $bayar += $tmb->saldo;
        }
        else{
          $hutang += $tmb->saldo;
        }
      }
      $bayarhutang = $bayar - $hutang;
//      set transaksi saat calon mahasiswa
      
      $keuangan_mahasiswa_jenis_biaya_sks = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_jenis_biaya_sks_masuk");
      foreach($jenis_biaya AS $jb){
        if($jb->id_frm_account != $keuangan_mahasiswa_jenis_biaya_sks[1]){
          $beban = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan", "price", array("id_course_category" => $pst['id_course_category'], "id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], "id_keuangan_mahasiswa_jenis_biaya" => $jb->id_keuangan_mahasiswa_jenis_biaya));
          $kirim[] = array(
              "nim"                                 => $pst['nim'],
              "id_keuangan_mahasiswa_jenis_virtual_account" => $jb->id_keuangan_mahasiswa_jenis_virtual_account,
              "id_keuangan_mahasiswa_jenis_biaya"   => $jb->id_keuangan_mahasiswa_jenis_biaya,
              "notransaksi"                         => $pst['notransaksi'],
              "saldo"                               => ($beban * $pecent/100),
              "pos"                                 => 1,
              "id_semester"                         => $pst['semester'],
              "tahun"                               => $pst['tahun_ajaran'],
              "tahunsemester"                       => $pst['tahun_ajaran'].$pst['semester'],
              "note"                                => "Biaya ".$jb->title,
              "create_date"                         => date("Y-m-d H:i:s")
          );

          $kredit[] = $beban * $pecent/100;
          $kredit_da[] = $jb->id_frm_account;

          $total_tanggungan += ($beban * $pecent/100);
        }
      }
      
//      biaya kuliah
      $jenis_biaya_kuliah = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_kuliah");
      $pecent_kuliah = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", "percent", 
            array("id_course_category" => $pst['id_course_category'], "id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], "semester" => 0));
      
      foreach($jenis_biaya_kuliah AS $jbk){
        $beban_kuliah = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan", "price", array("id_course_category" => $pst['id_course_category'], "id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], "id_keuangan_mahasiswa_jenis_biaya_kuliah" => $jbk->id_keuangan_mahasiswa_jenis_biaya_kuliah));
        $this->global_models->get_connect("zikula");
        $program_kuliah = $this->global_models->get_field("academicportal_mahasiswa", "program_kuliah", array("nim" => $pst['nim']));
        $this->global_models->get_connect("default");
        $kirim[] = array(
            "nim"                                 => $pst['nim'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $jbk->id_keuangan_mahasiswa_jenis_virtual_account,
            "id_keuangan_mahasiswa_jenis_biaya"   => "",
            "notransaksi"                         => $pst['notransaksi'],
            "saldo"                               => ($beban_kuliah * $pecent_kuliah/100),
            "pos"                                 => 1,
            "id_program_kuliah"                   => $program_kuliah,
            "id_kelompok"                         => $pst['id_kelompok_mahasiswa'],
            "id_semester"                         => $pst['semester'],
            "tahun"                               => $pst['tahun_ajaran'],
            "note"                                => "Biaya ".$jbk->title,
            "create_date"                         => date("Y-m-d H:i:s")
        );

        $kredit[] = $beban_kuliah * $pecent_kuliah/100;
        $kredit_da[] = $jbk->id_frm_account;

        $total_tanggungan += ($beban_kuliah * $pecent_kuliah/100);
      }
      
      $this->global_models->insert_batch("keuangan_mahasiswa_tanggungan", $kirim);
      
      $kirim_journal_log = array(
          "id_frm_journal"      => $this->mfrm->get_journal_current(),
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $total_tanggungan,
          "tanggal"             => date("Y-m-d"),
          "note"                => "Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim'])."'>{$pst['nim']}</a>",
      );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);

//      cek tunggakan
//      $tunggakan = $this->mfrm->get_tunggakan_mahasiswa_baru($ref_number);
      
      $this->global_models->get_connect("zikula");
      $id_fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $pst['id_course_category']));
//      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$id_fakultas);
      $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
      
      $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
              array("uid" => $pst['users']));
      
      if($bayarhutang <= 0){
//        masuk tunggakan semua
        $debit[] = $total_tanggungan;
        $debit_da[] = $id_piutang_mahasiswa[1];
        
        if(!$id_persyaratan_keuangan)
          $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => $pst['users']));
        else
          $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => $pst['users']));
        
      }
      else{
//        sebagian masuk piutang kelebihan dibayar
        $selisih = $bayarhutang - $total_tanggungan;
        if($selisih <= 0){
          $debit[] = $bayarhutang;
          $debit_da[] = $id_hutang_mahasiswa[1];
          
          $debit[] = abs($selisih);
          $debit_da[] = $id_piutang_mahasiswa[1];
          
          if(!$id_persyaratan_keuangan)
            $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => $pst['users']));
          else
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => $pst['users']));
          
        }
        else{
          $debit[] = $total_tanggungan;
          $debit_da[] = $id_hutang_mahasiswa[1];
          
          if(!$id_persyaratan_keuangan)
            $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => -1));
          else
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $pst['users'], "insert_by_who" => $pst['users'], "update_by_who" => $pst['users'], "delete_by_who" => -1));
          
        }
      }
      $this->global_models->get_connect("default");
      
      if($id_frm_journal_log){
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
        $result = "Berhasil";
      }
      
    }
    print $result;
    die;
  }
  
  function curl_sign_tanggungan_mahasiswa(){
    
    /**
     * kunci
     * type
     * nim
     * notransaksi
     * semester
     * tahun_ajaran
     * sks
     */
    $result = "Gagal";
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == 'nubusa'){
      
      $this->global_models->get_connect("zikula");
      $mahasiswa = $this->global_models->get("academicportal_mahasiswa", array("nim" => $pst['nim']));
      $student_categories = $this->global_models->get("academicportal_student_categories", array("users" => $mahasiswa[0]->pn_uid));
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $student_categories[0]->course_category));
      $this->global_models->get_connect("default");
      
      $jenis_biaya = $this->global_models->get("keuangan_mahasiswa_jenis_biaya");
      $pecent = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", "percent", 
            array("id_course_category" => $student_categories[0]->course_category, "id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "type" => $pst['type']));
      $total_tanggungan = 0;
      
      $keuangan_mahasiswa_jenis_biaya_sks = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_jenis_biaya_sks");
      foreach($jenis_biaya AS $jb){
//        if($jb->id_frm_account != $keuangan_mahasiswa_jenis_biaya_sks[1]){
          $beban = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan", "price", array("id_course_category" => $student_categories[0]->course_category, "id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "id_keuangan_mahasiswa_jenis_biaya" => $jb->id_keuangan_mahasiswa_jenis_biaya));
          if($jb->id_keuangan_mahasiswa_jenis_biaya == $keuangan_mahasiswa_jenis_biaya_sks[1]){
            $beban = $beban * $pst['sks'];
          }
          $this->global_models->get_connect("zikula");
          $program_kuliah = $this->global_models->get_field("academicportal_mahasiswa", "program_kuliah", array("nim" => $pst['nim']));
          $this->global_models->get_connect("default");
          $kirim[] = array(
              "nim"                                 => $pst['nim'],
              "id_keuangan_mahasiswa_jenis_virtual_account"   => $jb->id_keuangan_mahasiswa_jenis_virtual_account,
              "id_keuangan_mahasiswa_jenis_biaya"   => $jb->id_keuangan_mahasiswa_jenis_biaya,
              "notransaksi"                         => $pst['notransaksi'],
              "saldo"                               => ($beban * $pecent/100),
              "pos"                                 => 1,
              "id_program_kuliah"                   => $program_kuliah,
              "id_kelompok"                         => $mahasiswa[0]->kelompok_mahasiswa,
              "id_semester"                         => $pst['semester'],
              "tahun"                               => $pst['tahun_ajaran'],
              "tahunsemester"                       => $pst['tahun_ajaran'].$pst['semester'],
              "note"                                => "Biaya ".$jb->title,
              "create_date"                         => date("Y-m-d H:i:s")
          );

          $kredit[] = $beban * $pecent/100;
          $kredit_da[] = $jb->id_frm_account;

          $total_tanggungan += ($beban * $pecent/100);
//        }
      }
      
//      biaya kuliah
//      $jenis_biaya_kuliah = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_kuliah");
//      $pecent_kuliah = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", "percent", 
//            array("id_course_category" => $student_categories[0]->course_category, "id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "semester" => 0));
//      
//      foreach($jenis_biaya_kuliah AS $jbk){
//        $beban_kuliah = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan", "price", array("id_course_category" => $student_categories[0]->course_category, "id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "id_keuangan_mahasiswa_jenis_biaya_kuliah" => $jbk->id_keuangan_mahasiswa_jenis_biaya_kuliah));
//        $kirim[] = array(
//            "nim"                                 => $pst['nim'],
//            "id_keuangan_mahasiswa_jenis_biaya"   => "",
//            "notransaksi"                         => $pst['notransaksi'],
//            "saldo"                               => ($beban_kuliah * $pecent_kuliah/100),
//            "pos"                                 => 1,
//            "id_semester"                         => $pst['semester'],
//            "tahun"                               => $pst['tahun_ajaran'],
//            "note"                                => "Biaya ".$jbk->title,
//            "create_date"                         => date("Y-m-d H:i:s")
//        );
//
//        $kredit[] = $beban_kuliah * $pecent_kuliah/100;
//        $kredit_da[] = $jbk->id_frm_account;
//
//        $total_tanggungan += ($beban_kuliah * $pecent_kuliah/100);
//      }
      
      $this->global_models->insert_batch("keuangan_mahasiswa_tanggungan", $kirim);
      
      $kirim_journal_log = array(
          "id_frm_journal"      => $this->mfrm->get_journal_current(),
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $total_tanggungan,
          "tanggal"             => date("Y-m-d"),
          "note"                => "Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim'])."'>{$pst['nim']}</a>",
      );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);

//      cek tunggakan
      $tunggakan = $this->mfrm->get_tunggakan($pst['nim']);
      
//      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
//      $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
      $id_pembalik_kelebihan_bayar = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
      
      $this->global_models->get_connect("zikula");
      $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
              array("uid" => $mahasiswa[0]->pn_uid));
      
      if($tunggakan >= 0){
//        masuk tunggakan semua
        $debit[] = $total_tanggungan;
        $debit_da[] = $id_piutang_mahasiswa[1];
        
        if(!$id_persyaratan_keuangan)
          $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => $mahasiswa[0]->pn_uid));
        else
          $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => $mahasiswa[0]->pn_uid));
        
      }
      else{
//        sebagian masuk piutang kelebihan dibayar
        $selisih = $tunggakan + $total_tanggungan;
        if($selisih < 0){
          $debit[] = abs($selisih);
          $debit_da[] = $id_pembalik_kelebihan_bayar[1];
          
          $debit[] = $total_tanggungan - abs($selisih);
          $debit_da[] = $id_piutang_mahasiswa[1];
          
          if(!$id_persyaratan_keuangan)
            $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => $mahasiswa[0]->pn_uid));
          else
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => $mahasiswa[0]->pn_uid));
          
        }
        else{
          $debit[] = $total_tanggungan;
          $debit_da[] = $id_piutang_mahasiswa[1];
          
          if(!$id_persyaratan_keuangan)
            $this->global_models->insert("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => -1));
          else
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan), array("uid" => $mahasiswa[0]->pn_uid, "insert_by_who" => $mahasiswa[0]->pn_uid, "update_by_who" => $mahasiswa[0]->pn_uid, "delete_by_who" => -1));
          
        }
      }
      $this->global_models->get_connect("default");
      
      if($id_frm_journal_log){
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
        $result = "Berhasil";
      }
      
    }
    print $result;
    die;
  }
  
  function curl_lulus_calon_mahasiswa(){
    
    /**
     * kunci
     * id_kelompok_mahasiswa
     * id_course_category
     * ref_number
     * notransaksi
     * semester
     * tahun_ajaran
     * id_ao_applicant
     */
    $result = "Gagal";
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == 'nubusa'){
      $jenis_biaya_masuk = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_masuk");
      $total_tanggungan = 0;
      foreach($jenis_biaya_masuk AS $jbm){
        $saldo = $this->global_models->get("keuangan_mahasiswa_setting_biaya_angkatan", 
            array(
                "id_kelompok_mahasiswa"                     => $pst['id_kelompok_mahasiswa'], 
                "id_keuangan_mahasiswa_jenis_biaya_masuk"   => $jbm->id_keuangan_mahasiswa_jenis_biaya_masuk,
                "id_course_category"                        => $pst['id_course_category']
                ));
        $kirim[] = array(
            "nim"                                 => $pst['ref_number'],
            "id_ao_applicant"                     => $pst['id_ao_applicant'],
            "id_keuangan_mahasiswa_jenis_biaya_masuk" => $jbm->id_keuangan_mahasiswa_jenis_biaya_masuk,
            "id_keuangan_mahasiswa_jenis_virtual_account" => $jbm->id_keuangan_mahasiswa_jenis_virtual_account,
            "notransaksi"                         => $pst['notransaksi'],
            "saldo"                               => $saldo[0]->price,
            "pos"                                 => 1,
            "id_semester"                         => $pst['semester'],
            "tahun"                               => $pst['tahun_ajaran'],
            "note"                                => "Biaya Awal ".$jbm->title,
            "create_date"                         => date("Y-m-d H:i:s")
        );
        
        $kredit[] = $saldo[0]->price;
        $kredit_da[] = $jbm->id_frm_account;
        
        $total_tanggungan += $saldo[0]->price;
      }
      
      $data_sks = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_masuk_sks", 
              array("id_course_category" => $pst['id_course_category'], "id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa']));
      
      $keuangan_mahasiswa_virtual_account_biaya_kuliah = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_virtual_account_biaya_kuliah");
      
      $kirim[] = array(
          "nim"                                 => $pst['ref_number'],
          "id_ao_applicant"                     => $pst['id_ao_applicant'],
          "id_keuangan_mahasiswa_jenis_biaya_masuk" => 0,
          "id_keuangan_mahasiswa_jenis_virtual_account" => $keuangan_mahasiswa_virtual_account_biaya_kuliah[1],
          "notransaksi"                         => $pst['notransaksi'],
          "saldo"                               => ($data_sks[0]->price * $data_sks[0]->sks),
          "pos"                                 => 1,
          "id_semester"                         => $pst['semester'],
          "tahun"                               => $pst['tahun_ajaran'],
          "note"                                => "Biaya Awal SKS",
          "create_date"                         => date("Y-m-d H:i:s")
      );
      
      $keuangan_mahasiswa_jenis_biaya_sks = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_jenis_biaya_sks_masuk");
      
      $kredit[] = ($data_sks[0]->price * $data_sks[0]->sks);
      $kredit_da[] = $keuangan_mahasiswa_jenis_biaya_sks[1];
      
      $total_tanggungan += ($data_sks[0]->price * $data_sks[0]->sks);
      
      $this->global_models->insert_batch("keuangan_mahasiswa_baru_tanggungan", $kirim);
      
      $this->global_models->get_connect("zikula");
      $this->global_models->update("academicportal_ao_applicant", array("id" => $pst['id_ao_applicant']), array("status_uang_pangkal" => 1));
      $id_fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $pst['id_course_category']));
      $this->global_models->get_connect("default");
      
      $kirim_journal_log = array(
          "id_frm_journal"      => $this->mfrm->get_journal_current(),
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $total_tanggungan,
          "tanggal"             => date("Y-m-d"),
          "note"                => "Biaya Tanggungan Mahasiswa Baru <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa-baru/".$pst['ref_number'])."'>{$pst['ref_number']}</a>",
      );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);
      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$id_fakultas);
      if($id_frm_journal_log){
        $debit[] = $total_tanggungan;
        $debit_da[] = $id_piutang_mahasiswa[1];
        
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
        $result = "Berhasil";
      }
      
    }
    print $result;
    die;
  }
  
  function curl_sign_keuangan(){
    /**
     * nim
     * users
     * id_keuangan_mahasiswa_jenis_biaya
     * notransaksi
     * saldo
     * pos
     * semester
     * tahun_ajaran
     * id_sem_ta
     * note
     * kunci
     */
    $pst = $this->input->post(NULL);
    if($pst['kunci']){
      $this->global_models->get_connect("zikula");
      $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $pst['nim']));
      $this->global_models->get_connect("default");
      $kirim = array(
          "nim"                                 => $pst['nim'],
          "id_keuangan_mahasiswa_jenis_biaya"   => $pst['id_keuangan_mahasiswa_jenis_biaya'],
          "id_program_kuliah"                   => $program_kuliah[0]->program_kuliah,
          "id_kelompok"                         => $program_kuliah[0]->kelompok_mahasiswa,
          "notransaksi"                         => $pst['notransaksi'],
          "saldo"                               => $pst['saldo'],
          "pos"                                 => $pst['pos'],
          "id_semester"                         => $pst['semester'],
          "tahun"                               => $pst['tahun_ajaran'],
          "tahunsemester"                       => $pst['tahun_ajaran'].$pst['semester'],
          "note"                                => $pst['note'],
          "create_date"                         => date("Y-m-d H:i:s")
      );
      $id_keuangan_mahasiswa_tanggungan = $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim);
      
      $kirim_journal_log = array(
          "id_frm_journal"      => $this->mfrm->get_journal_current(),
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $pst['saldo'],
          "tanggal"             => date("Y-m-d"),
          "note"                => "Biaya ".$this->global_models->get_field("keuangan_mahasiswa_jenis_biaya", "title", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_keuangan_mahasiswa_jenis_biaya']))." <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim'])."'>{$pst['nim']}</a>",
      );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);
      
      $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
      $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
      
      if($id_frm_journal_log){
        $debit[] = $pst['saldo'];
        $debit_da[] = $id_pembayaran_mahasiswa[1];

        $kredit[] = $pst['saldo'];
        $kredit_da[] = $id_hutang_mahasiswa[1];
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
      }
      
      $total_debit = $this->global_models->get_query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$pst['nim']}' AND A.pos = 1
        ");

      $total_kredit = $this->global_models->get_query("
        SELECT SUM(A.saldo) AS saldo
        FROM keuangan_mahasiswa_tanggungan AS A
        WHERE A.nim = '{$pst['nim']}' AND A.pos = 2
        ");

      $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
      
      if($total_akhir < 0){
        $this->global_models->get_connect("zikula");
        $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
                array("id_sem_ta" => $pst['id_sem_ta'], "uid" => $pst['users']));

        if(!$id_persyaratan_keuangan){
          $this->global_models->insert("academicportal_persyaratan_keuangan", 
          array("uid" => $pst['users'], "id_sem_ta" => $pst['id_sem_ta'], "insert_by_who" => $pst['users'], "update_by_who" => -1, 
              "delete_by_who" => $pst['users']));
        }
        else{
          $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan),
          array("uid" => $pst['users'], "id_sem_ta" => $pst['id_sem_ta'], "insert_by_who" => $pst['users'], "update_by_who" => -1, 
              "delete_by_who" => $pst['users']));
        }

        $this->global_models->get_connect("default");
      }
      
      print "Berhasil";die;
    }
    print "Gagal";
    die;
  }
  
  function curl_get_account(){
    /**
     * jenis_biaya
     * kunci
     */
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == "nubusa"){
      $jenis_biaya = $this->global_models->get("frm_account", array("title" => $pst['account']));
      print "[]".$jenis_biaya[0]->id_frm_account."[]";
      die;
    }
    print "Gagal";
    die;
  }
  
  function curl_get_all_account(){
    /**
     * jenis_biaya
     * kunci
     */
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == "nubusa"){
      $jenis_biaya = $this->global_models->get_dropdown("frm_account", "id_frm_account", "title");
      print serialize($jenis_biaya);
      die;
    }
    print "Gagal";
    die;
  }
  
  function curl_sign_journal(){
    /**
     * jenis_biaya
     * kunci
     */
    $pst = $this->input->post(NULL);
    if($pst['kunci'] == "nubusa"){
      $kirim = array(
          "id_frm_journal"      => $this->mfrm->get_journal_current(),
          "notransaksi"         => $pst['notransaksi'],
          "saldo"               => $pst['total'],
          "tanggal"             => $pst['tanggal'],
          "note"                => $pst['note'],
      );
      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);

      if($id_frm_journal_log){

        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, unserialize($pst['debit']), unserialize($pst['id_account_debit']), unserialize($pst['kredit']), unserialize($pst['id_account_kredit']));

        print "Berhasil";
        die;
      }
      else{
        print "Gagal";
        die;
      }
    }
    print "Gagal";
    die;
  }
  
  function mahasiswa($id_ao_applicant){
    
    $this->global_models->get_connect("zikula");
    $app = $this->global_models->get("academicportal_ao_applicant", array("id" => $id_ao_applicant));
//    users
    $users = array(
        "pn_name"     => $app[0]->fullname,
        "pn_uname"    => 999999988,
        "pn_email"    => '999999988@academic.com',
        "pn_pass"     => md5("1")
    );
    $id_users = $this->global_models->insert("academicportal_users", $users);
    
//    mahasiswa
    $mahasiswa = array(
        "nim"                 => 999999988,
        "nama"                => $app[0]->fullname,
        "pn_uid"              => $id_users,
        "status_mahasiswa"    => "A",
        "kelompok_mahasiswa"  => 49,
        "ao_applicant_id"     => $id_ao_applicant,
        "tahun_masuk"         => 2013
    );
    $id_mahasiswa = $this->global_models->insert("academicportal_mahasiswa", $mahasiswa);
    
//    student_categories
    $student_categories = array(
        "users"             => $id_users,
        "course_category"   => 19,
        "student_status"    => "A"
    );
    $id_student_categories = $this->global_models->insert("academicportal_student_categories", $student_categories);
    
    $this->global_models->get_connect("default");
    die;
    
  }
  
  function curl_get_tunggakan_komponen(){
    $pst = $this->input->post();
    if($pst['kunci'] == "nubusa"){
      $komponen = $this->global_models->get("keuangan_mahasiswa_jenis_virtual_account", array("code" => $pst['code']));

      $tanggungan = $this->global_models->get_query("
        SELECT *
        FROM keuangan_mahasiswa_tanggungan
        WHERE id_keuangan_mahasiswa_jenis_virtual_account = {$komponen[0]->id_keuangan_mahasiswa_jenis_virtual_account}
          AND nim = '{$pst['nim']}'
        ORDER BY create_date ASC
        ");
      $ser_tanggungan = serialize($tanggungan);
      print $ser_tanggungan;
    }
    else{
      print "Gagal";
    }
//    $this->debug(unserialize($ser_tanggungan));
    
    die;
  }
  
  function curl_biaya_formulir(){
    $pst = $this->input->post();
    if($pst['kunci'] == "nubusa"){
      $id_pendapatan_formulir_pendaftaran = $this->nbscache->get_explode("variable", "id_pendapatan_formulir_pendaftaran");
      $id_formulir_pendaftaran = $this->nbscache->get_explode("variable", "id_formulir_pendaftaran");
      $keuangan_mahasiswa_formulir_pendaftaran = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_formulir_pendaftaran");
      
      $kirim = array(
              "id_frm_journal"      => $this->mfrm->get_journal_current(),
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $keuangan_mahasiswa_formulir_pendaftaran[1],
              "tanggal"             => $pst['tanggal'],
              "note"                => "Formulir Pendaftaran <a href='".site_url("keuangan-mahasiswa/mahasiswa-baru/{$pst['no_ref']}")."'>{$pst['no_ref']}</a>",
          );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
      if($id_frm_journal_log){ 
        $debit[] = $keuangan_mahasiswa_formulir_pendaftaran[1];
        $debit_da[] = $id_formulir_pendaftaran[1];

        $kredit[] = $keuangan_mahasiswa_formulir_pendaftaran[1];
        $kredit_da[] = $id_pendapatan_formulir_pendaftaran[1];
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
      }
      
      $this->global_models->get_connect("zikula");
      $this->global_models->update("academicportal_ao_applicant", array("ref_number" => $pst['no_ref']), array("status_pembayaran_formulir" => 1));
      $this->global_models->get_connect("default");
      print "Berhasil";
    }
    else{
      print "Gagal";
    }
//    $this->debug(unserialize($ser_tanggungan));
    
    die;
  }
  
  function curl_get_report_all_kelompok(){
    $pst = $this->input->post();
    if($pst['kunci'] == "nubusa"){
      
      $this->global_models->get_connect("zikula");
      
      if($pst['id_program_kuliah'] != 0){
        $where_prokul = " WHERE A.id_program_kuliah = {$pst['id_program_kuliah']}";
      }
      else{
        $where_prokul = "";
      }
      
      $prokul = $this->global_models->get_query("
        SELECT A.id_program_kuliah, B.name, A.nama_program
        FROM academicportal_program_kuliah AS A
        LEFT JOIN academicportal_course_categories AS B ON A.category = B.id
        {$where_prokul}
        ");
      
      if($pst['id_kelompok'] != 0){
        $where_kelompok = " WHERE A.id_kelompok_mahasiswa= {$pst['id_kelompok']}";
      }
      else{
        $where_kelompok = "";
      }
        
      $kelompok_drop = $this->global_models->get_query("
        SELECT *
        FROM academicportal_kelompok_mahasiswa
        {$where_kelompok}
        ");
      
      $this->global_models->get_connect("default");
      
      foreach($prokul AS $prkl){
        foreach ($kelompok_drop AS $kd){
          $her = $this->global_models->get_field("keuangan_mahasiswa_tanggungan", "SUM(saldo)", 
                  array(
                      "id_program_kuliah" => $prkl->id_program_kuliah,
                      "id_kelompok"       => $kd->id_kelompok_mahasiswa,
                      "id_keuangan_mahasiswa_jenis_biaya" => 0
                      ));
          $kuliah = $this->global_models->get_query("
            SELECT SUM(saldo) AS price
            FROM `keuangan_mahasiswa_tanggungan` 
            WHERE `id_program_kuliah` = {$prkl->id_program_kuliah} 
              AND `id_kelompok` = {$kd->id_kelompok_mahasiswa}
              AND pos = 1 
              AND (id_keuangan_mahasiswa_jenis_biaya <> 0 OR id_keuangan_mahasiswa_jenis_biaya IS NULL)
            ");
          $pendapatan = $this->global_models->get_field("keuangan_mahasiswa_tanggungan", "SUM(saldo)", 
                  array(
                      "id_program_kuliah" => $prkl->id_program_kuliah,
                      "id_kelompok"       => $kd->id_kelompok_mahasiswa,
                      "pos"               => 2
                      ));
          $hasil[] = array(
              "prokul"      => $prkl->name." - ".$prkl->nama_program,
              "angkatan"    => $kd->nama_kelompok,
              "id_kelompok_mahasiswa" => $kd->id_kelompok_mahasiswa,
              "id_program_kuliah" => $prkl->id_program_kuliah,
              "her_reg"     => $her,
              "kuliah"      => $kuliah[0]->price,
              "pendapatan"  => $pendapatan,
          );
        }
      }
      
      print serialize($hasil);
    }
    else{
      print "Gagal";
    }
//    $this->debug(unserialize($ser_tanggungan));
    
    die;
  }
  
  function curl_get_report_mahasiswa(){
    $pst = $this->input->post();
    if($pst['kunci'] == "nubusa"){
      $data = $this->global_models->get_query("
        SELECT A.*, B.title AS jenis_biaya
        FROM keuangan_mahasiswa_tanggungan AS A
        LEFT JOIN keuangan_mahasiswa_jenis_biaya AS B ON A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya
        WHERE A.id_program_kuliah = {$pst['id_program_kuliah']} AND A.id_kelompok = {$pst['id_kelompok']} AND A.nim = '{$pst['nim']}'
        ORDER BY A.create_date DESC
        ");
      foreach($data AS $dt){
        $hasil[] = array(
            "id_semester"     => $dt->id_semester,
            "tahun"           => $dt->tahun,
            "note"            => $dt->note,
            "create_date"     => $dt->create_date,
            "jenis_biaya"     => $dt->jenis_biaya,
            "jenis"           => $dt->pos,
            "saldo"           => $dt->saldo,
            "notransaksi"     => $dt->notransaksi,
        );
      }
      print serialize($hasil);
    }
    else{
      print "Gagal";
    }
//    $this->debug(unserialize($ser_tanggungan));
    
    die;
  }
  
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */